package com.frx.sort.mystack;

import java.util.Arrays;

/**
 * Created with IDEA
 *
 * @author: xrf
 * @Date: 2019/10/28
 * @Time: 10:29
 */
public class ArrayStack {
    int[] arr;
    int count;
    int[] minArr;
    int min;
    int n;
    public ArrayStack(int n){
        this.arr = new int[n];
        this.minArr = new int[n];
        this.n = n;
        this.count = 0;
        this.min = 0;
    }

    public static void main(String[] args){
        ArrayStack stack = new ArrayStack(5);
        stack.push(3);
        stack.push(2);
        stack.push(1);
        stack.push(4);
        stack.push(5);
        System.out.println(stack.getMin());
        System.out.println(stack.pop());
        System.out.println(stack.pop());
        System.out.println(stack.pop());
        System.out.println(stack.getMin());
/*        System.out.println(stack.pop());
        System.out.println(stack.getMin());
        System.out.println(stack.pop());
        System.out.println(stack.getMin());
        System.out.println(stack.pop());*/

    }
    public void push(int a){
        if (count == n){
            throw new IllegalArgumentException("stack is full !");
        }
        if (min == 0){
            minArr[min++] = a;
            arr[count++] = a;
        } else {
            if (minArr[min-1] > a){
                minArr[min++] = a;
            }else {
                minArr[min] = minArr[min-1];
                min = min + 1;
            }
            arr[count++] = a;
        }
        System.out.println(Arrays.toString(arr));
        System.out.println(Arrays.toString(minArr));

    }

    public int pop(){
        if (count == 0){
            throw new IllegalArgumentException("stack is empty !");
        }
        int result = arr[--count];
        min--;
        return result;
    }

    public int getMin(){
        if (min == 0){
            throw new IllegalArgumentException("stack is empty !");
        }
        int minElement = minArr[min-1];
        return minElement;
    }


}
